package com.tencent.wxpay.imagefacesign;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.tencent.xinyapayface.facePay.FacePayCtr;
import com.tencent.xinyapayface.util.LogUtil;
import com.tencent.xinyapayface.util.UpgradeUtils;

public class PayActivity extends AppCompatActivity {

    private FacePayCtr facePayCtr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay);
        facePayCtr = new FacePayCtr(this);
    }

    /**
     * 人脸支付
     *
     * @param view
     */
    public void facePay(View view) {
        boolean checkAppExit = facePayCtr.checkAppExit();
        if (!checkAppExit){
            facePayCtr.checkDownloadUrlRight(new UpgradeUtils.CheckUpgradeListener() {
                @Override
                public void check(boolean success) {
                    if (success){
                        new AlertDialog.Builder(PayActivity.this)
                                .setTitle("未安装微信刷脸支付app，无法刷脸支付，是否立即下载")
                                .setNegativeButton("否", null)
                                .setPositiveButton("是", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        facePayCtr.downloadFacePayApk();
                                    }
                                })
                                .create()
                                .show();
                        facePayCtr.downloadFacePayApk();
                    }  else {
                        LogUtil.LogShitou("PayActivity--check", "下载地址失效");
                    }
                }
            });
            return;
        }
        String tradeNo = "" + System.currentTimeMillis() ;
        facePayCtr.setOnResultListener(new FacePayCtr.OnResultListener() {
            @Override
            public void success(String order_no) {
                Log.e("PayActivity--success", "支付成功");
            }

            @Override
            public void error(String msg) {
                Log.e("PayActivity--error", msg);
            }
        });
        facePayCtr.pay("12345", 3, 1, tradeNo, "测试", "", "");
    }

    /**
     * 扫码支付
     * @param view
     */
    public void scanPay(View view){
        boolean checkAppExit = facePayCtr.checkAppExit();
        if (!checkAppExit){
            facePayCtr.checkDownloadUrlRight(new UpgradeUtils.CheckUpgradeListener() {
                @Override
                public void check(boolean success) {
                    if (success){
                        new AlertDialog.Builder(PayActivity.this)
                                .setTitle("未安装微信刷脸支付app，无法刷脸支付，是否立即下载")
                                .setNegativeButton("否", null)
                                .setPositiveButton("是", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        facePayCtr.downloadFacePayApk();
                                    }
                                })
                                .create()
                                .show();
                        facePayCtr.downloadFacePayApk();
                    }  else {
                        LogUtil.LogShitou("PayActivity--check", "下载地址失效");
                    }
                }
            });
            return;
        }
        facePayCtr.scanCode();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        new FacePayCtr(this).releasePayFace();
    }
}
